<template>
  <div class="comments">
    <myheader title="精彩评论">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </myheader>
    <div class="lists">
      <div v-for="(v, i) in articleCommentList" :key="i" class="item">
        <div class="head">
          <img :src="v.user.head_img" alt />
          <div>
            <p>{{ v.user.nickname }}</p>
            <span>{{ $moment(v.create_date).fromNow() }}</span>
          </div>
          <span>回复</span>
        </div>
        <commentItem :parent="v.parent" v-if="v.parent"></commentItem>
        <div class="text">{{ v.content }}</div>
      </div>
    </div>
    <!--底部评论模块 -->
    <!--传值过去给子组件  -->
    <articlebottom :post="article" @click="refresh"></articlebottom>
  </div>
</template>

<script>
import myheader from "@/components/myheader.vue";
import articlebottom from "@/components/articlebottom";
import { commentList, particulars } from "@/apis/article.js";
import commentItem from "@/components/commentitem.vue";
import axios from "@/utils/myaxios.js";
export default {
  data() {
    return {
      articleCommentList: [],
      // 文章详情
      article: {},
    };
  },
  components: {
    myheader,
    commentItem,
    articlebottom,
  },
  methods: {
    async refresh() {
      // 用户点评论完就重新加载页面刷新 数据回到最顶上显示
      let res = await commentList(this.$route.params.id, { pageSize: 100 });
      console.log(res);
      this.articleCommentList = res.data.data;
      // 评论完就回到最顶部
      window.scrollTo(0, 0);
      // // 这个复杂的遍历就是为了给精彩评论添加默认的图片
      for (let i = 0; i < this.articleCommentList.length; i++) {
        if (this.articleCommentList[i].user.head_img === "") {
          // 但前的值拼接基准路劲 和默认的图片
          this.articleCommentList[i].user.head_img =
            axios.defaults.baseURL + "/uploads/image/luban.jpeg";
        }
      }
    },
  },
  async mounted() {
    // 文章详情
    let result = await particulars(this.$route.params.id);
    // console.log(result);
    this.article = result.data.data;

    // 获取用户所有的评论
    let res = await commentList(this.$route.params.id, { pageSize: 100 });
    console.log(res);
    this.articleCommentList = res.data.data;

    // // 这个复杂的遍历就是为了给精彩评论添加默认的图片
    for (let i = 0; i < this.articleCommentList.length; i++) {
      if (this.articleCommentList[i].user.head_img === "") {
        // 但前的值拼接基准路劲 和默认的图片
        this.articleCommentList[i].user.head_img =
          axios.defaults.baseURL + "/uploads/image/luban.jpeg";
      }
    }
  },
};
</script>

<style lang='less' scoped>
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px 40px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>